home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / Rectangle.h < prev    next >
C/C++ Source or Header  |  1990-12-03  |  4KB  |  167 lines

  1. #ifndef Rectangle_First
  2. #ifdef __GNUG__
  3. #pragma once
  4. #endif
  5. #define Rectangle_First
  6.  
  7. #include "Metric.h"
  8.  
  9. class Rectangle {
  10. public:
  11.     Point origin;
  12.     Point extent;
  13.       
  14. public:
  15.     
  16.     Rectangle();
  17.  
  18.     Rectangle(short x, short y, short w, short h);
  19.  
  20.     Rectangle(Point o, Point e);
  21.     
  22.     Rectangle(Point e);
  23.  
  24.     Rectangle(short w, short h);
  25.     
  26.     int Width() const
  27.     { return extent.x; }
  28.  
  29.     int Height() const
  30.     { return extent.y; }
  31.  
  32.     int Left() const
  33.     { return origin.x; }
  34.  
  35.     int Top() const
  36.     { return origin.y; }
  37.     
  38.     Point Center() const
  39.     { return Point(origin.x+extent.x/2, origin.y+extent.y/2); }
  40.  
  41.     Point NW() const
  42.     { return origin; }
  43.     
  44.     Point N() const
  45.     { return Point(origin.x+extent.x/2, origin.y); }
  46.  
  47.     Point NE() const
  48.     { return Point(origin.x+extent.x-1, origin.y); }
  49.  
  50.     Point E() const
  51.     { return Point(origin.x+extent.x-1, origin.y+extent.y/2); }
  52.     
  53.     Point SE() const
  54.     { return origin + extent - 1; }
  55.     
  56.     Point S() const
  57.     { return Point(origin.x+extent.x/2, origin.y+extent.y-1); }
  58.     
  59.     Point SW() const
  60.     { return Point(origin.x, origin.y+extent.y-1); }
  61.  
  62.     Point W() const
  63.     { return Point(origin.x, origin.y+extent.y/2); }
  64.         
  65.     friend Rectangle NormRect(Point p1, Point p2);
  66.     
  67.     friend Rectangle BoundingBox(int, Point *pts, Point *npts= 0);
  68.  
  69.     int Area () const
  70.     { return extent.x * extent.y; }
  71.  
  72.     Rectangle &Moveby (Point p)
  73.     { origin+= p; return *this; }
  74.     
  75.     friend Rectangle Moveby (Rectangle r, Point p)
  76.     { r.origin+= p; return r; }
  77.     
  78.     Rectangle operator+= (Point p)
  79.     { origin+= p; return *this; }
  80.     
  81.     Rectangle operator+ (Point p)
  82.     { return Rectangle(origin+p, extent); }
  83.     
  84.     Rectangle operator- (Point p)
  85.     { return Rectangle(origin-p, extent); }
  86.     
  87.     Rectangle operator-= (Point p)
  88.     { origin-= p; return *this; }
  89.  
  90.     void Moveto (Point p)
  91.     { origin= p; }
  92.  
  93.     void Scaleby (Point p)
  94.     { extent*= p; }
  95.  
  96.     friend bool operator== (const Rectangle &r1, const Rectangle &r2)
  97.     { return (bool) (r1.origin == r2.origin && r1.extent == r2.extent); }
  98.  
  99.     friend bool operator!= (const Rectangle &r1, const Rectangle &r2)
  100.     { return (bool) (r1.origin != r2.origin || r1.extent != r2.extent); }
  101.     
  102.     friend Rectangle Union(const Rectangle &r1, const Rectangle &r2);
  103.  
  104.     friend Rectangle Inter(const Rectangle &r1, const Rectangle &r2);
  105.  
  106.     friend int Difference(Rectangle *rp, const Rectangle &r1, const Rectangle &r2);
  107.     
  108.     Rectangle Intersect(const Rectangle &r);
  109.  
  110.     bool Clip(const Rectangle &r);
  111.     
  112.     bool ContainsRect(const Rectangle &r) const;
  113.     bool OvalContainsRect(const Rectangle &r) const;
  114.     
  115.     Rectangle Inset(Point p);
  116.  
  117.     Rectangle Expand(Point p);
  118.     
  119.     Rectangle Merge(const Rectangle &r);
  120.  
  121.     bool ContainsPoint(Point p) const
  122.     { return (bool) (p >= origin && p < origin + extent); }
  123.  
  124.     bool Intersects(const Rectangle &r) const;
  125.           
  126.     bool IsEmpty() const
  127.     { return (bool) (extent.x <= 0 || extent.y <= 0); }
  128.         
  129.     bool IsNotEmpty() const
  130.     { return (bool) (extent.x > 0 && extent.y > 0); }
  131.  
  132.     Point Constrain(Point p);
  133.     
  134.     friend Point ConstrainMoveRect(const Rectangle &r1, const Rectangle &r2, Point delta);
  135.  
  136.     Point AmountToTranslateWithin(const Rectangle &r) const;
  137.  
  138.     int PointToAngle(Point) const;
  139.     
  140.     Point AngleToPoint(int) const;
  141.     Point OvalAngleToPoint(int) const;
  142.         
  143.     Rectangle WedgeBBox(int s, int e);
  144.     
  145.     //         7|0|1
  146.     //        -------
  147.     //         6| |2
  148.     //        -------
  149.     //         5|4|3
  150.  
  151.     int PointToCorner(Point p) const;
  152.  
  153.     Point CornerToPoint(int n) const;
  154.     
  155.     friend ostream& operator<< (ostream &s, const Rectangle &r);
  156.            
  157.     friend istream& operator>> (istream &s, Rectangle &r);
  158.     
  159.     char *AsString() const;
  160. };
  161.  
  162. SimpleMetaDef(Rectangle);
  163.  
  164. extern const Rectangle gRect0;
  165.  
  166. #endif Rectangle_First
  167.